package ljt.music.ui.playlist;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ljt.music.bean.PlayList;
import ljt.music.bean.SreachBean;
import ljt.music.request.ApiService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @author jiangtao
 * @date 2017/12/21
 */

public class PlayListModel {
  private static final String BASE_URL = "http://yyk.zzbssb.com/";
  private Call<PlayList> call;
  private Call<List<SreachBean>> searchCall;

  public void request(String id, String types, Callback<PlayList> callback) {
    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
//                .addConverterFactory(ScalarsConverterFactory.create())
        .addConverterFactory(GsonConverterFactory.create())
        .build();

    ApiService apiService = retrofit.create(ApiService.class);
    Map<String, Object> map = new HashMap<>();
    map.put("types", types);
    map.put("id", id);
    call = apiService.queryPlayList(map);
    call.enqueue(callback);
  }

  public void search(String types, String count, String source, String pages, String name, Callback<List<SreachBean>> callback) {
    Retrofit retrofit = new Retrofit.Builder()
        .baseUrl(BASE_URL)
//                .addConverterFactory(ScalarsConverterFactory.create())
        .addConverterFactory(GsonConverterFactory.create())
        .build();

    ApiService apiService = retrofit.create(ApiService.class);
    Map<String, Object> map = new HashMap<>();
    map.put("types", types);
    map.put("count", count);
    map.put("source", source);
    map.put("pages", pages);
    map.put("name", name);
    searchCall = apiService.search(map);
    searchCall.enqueue(callback);
  }

  public void interruptHttp() {
    if (call != null && !call.isCanceled()) {
      call.cancel();
    }
  }
}
